<template>
  <div v-loading="showLoading" class="login-container">
    <div class="login-logo"></div>
    <div v-if="show" class="loginShow">
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
        <div class="form-container">
          <p class="title">欢迎登录</p>
          <p class="summary">物流大掌柜</p>
          <el-form-item prop="username">
            <span class="item-name">手机号</span>
            <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="请输入手机号"/>
          </el-form-item>
          <el-form-item prop="password">
            <span class="item-name">密码</span>
            <el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="请输入密码" @keyup.enter.native="handleLogin"/>
          </el-form-item>
          <!-- 验证码 captchaEnabled -->
          <el-form-item prop="code" v-if="captchaEnabled">
            <span class="item-name">验证码</span>
            <el-input v-model="loginForm.code" auto-complete="off" placeholder="请输入验证码"
              @keyup.enter.native="handleLogin">
            </el-input>
            <div class="login-code">
              <img :src="codeUrl" @click="getCode" class="login-code-img"/>
            </div>
          </el-form-item>
          <el-button class="btn-login" :loading="loading" size="medium" type="primary" @click.native.prevent="handleLogin">
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
          <div class="remember-foot">
            <!-- <el-checkbox v-model="loginForm.rememberMe">记住密码</el-checkbox> -->
             <span @click="handleGoForgetPage">忘记密码</span>
             <span @click="handleGoRegister">注册</span>
          </div>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      loginForm: {
        username: "",
        password: "",
        rememberMe: true,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        code: [{ required: false, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined,
      show: true,
      showLoading: false,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCode();
    this.getCookie();
    const username = this.$route.query.username;
    const password = this.$route.query.pwd;

    if (username && password) {  // 检查是否存在用户名和密码
      this.showLoading = true
      this.show = false
      this.loginForm.username = username
      this.loginForm.password = password
      this.$store.dispatch("Login", this.loginForm).then(() => {
        this.$router.push({ path: this.redirect || "/" }).catch(() => { });
        this.showLoading = false
      }).catch(() => {
        this.loading = false;
        if (this.captchaEnabled) {
          // this.getCode();
        }
      });
    }
  },
  methods: {
    handleGoForgetPage() {
      this.$router.push({ path: "/forgetPwd" });
    },
    handleGoRegister() {
      this.$router.push({ path: "/register" });
    },
    getCode() {
      getCodeImg().then(res => {
        if (res && res.data) {
          this.codeUrl = `${res.data}?_=${new Date().getTime()}`;
          // this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/" }).catch(() => { });
          }).catch(() => {
            this.loading = false;
            if (this.captchaEnabled) {
              this.getCode();
            }
          });
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">

</style>
